import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig({
  plugins: [vue()],
  base: '/imgmanager/',  // 添加项目名前缀
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://111.229.88.2:8000',
        changeOrigin: true
      }
    }
  },
  build: {
    outDir: 'dist',  // 改回默认的 dist 目录
    assetsInlineLimit: 4096,  // 4kb以下的资源内联为base64
    assetsDir: 'assets',  // 静态资源目录
    sourcemap: false,  // 不生成 sourcemap
    minify: 'esbuild',  // 使用 esbuild 压缩
    esbuildOptions: {
      drop: ['console', 'debugger']  // 移除 console 和 debugger
    },
    rollupOptions: {
      output: {
        chunkFileSize: 1500000,  // 代码分块大小
        manualChunks: {
          'vendor': ['vue', 'axios']  // 第三方库单独打包
        },
        assetFileNames: (assetInfo) => {
          const info = assetInfo.name.split('.')
          let extType = info[info.length - 1]
          if (/\.(png|jpe?g|gif|svg|webp|ico)(\?.*)?$/.test(assetInfo.name)) {
            extType = 'img'
          }
          return `static/${extType}/[name]-[hash][extname]`
        },
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
      }
    }
  }
}) 